# Maintainer: David Runge <dvzrv@archlinux.org>

_name=ADLplug
pkgbase=adlplug
pkgname=(adlplug{,-{lv2,standalone,vst}} opnplug{,-{lv2,standalone,vst}})
pkgver=1.0.2
pkgrel=10
pkgdesc="FM synthesizer plugins, based on OPL3 and OPN2 sound chip emulations"
arch=(loong64 x86_64)
url="https://github.com/jpcima/ADLplug"
license=(
  Boost
  GPL2
  GPL3
  LGPL2.1
)
# TODO: devendor simpleini, if feasible
makedepends=(
  cmake
  fmt
  freetype2
  jack
  liblo
  libxrandr
  libxinerama
  libxcursor
)
source=(
  https://github.com/jpcima/$pkgbase/releases/download/v$pkgver/$_name-$pkgver.tar.xz
  $pkgbase-1.0.1-gcc9.patch::https://github.com/WeAreROLI/JUCE/commit/4e0adb2af8b424c43d22bd431011c9a6c57d36b6.patch
)
sha512sums=('53192a622de528d4dd06dbd8411b5d1ba8346e9f181fd160fa14c2cf402a819ff354212ced59e13b11738ef6bf696cb0927a67c2e97ef94ade70acb29807f5cc'
            '9afe9d17244bb78726757c0eb1f23e2f29a98121728064653e758b8f009f7f0edeb40cd630d81fe7e4345bc657ddfbf776115e022d10e230fabf831bf04d9951')
b2sums=('ea049a6373fdd05f6401766f077819de704748055569a67aabb27e6064d922a28f635d8322914a5c418875b09a0152efa3ac4a3ae32400b12e31ae10166ceffb'
        '39409b3e4b3a7eec1e58c106cfea6b45e91dea7da13c28dbca27391f1157f9e8bcc9e898b9b49c063d9119c6f92cf5b378e8fba42d5ab8ad1c5ca94a1d541b4c')

_pick() {
  local p="$1" f d; shift
  for f; do
    d="$srcdir/$p/${f#$pkgdir/}"
    mkdir -p "$(dirname "$d")"
    mv "$f" "$d"
    rmdir -p --ignore-fail-on-non-empty "$(dirname "$f")"
  done
}

prepare() {
  # fix build with gcc9: https://github.com/jpcima/ADLplug/issues/54
  patch -Np1 -d$_name-$pkgver/thirdparty/JUCE -i ../../../$pkgbase-1.0.1-gcc9.patch
}

build() {
  local cmake_options_adlplug=(
    -B build-adlplug
    -D CMAKE_INSTALL_PREFIX=/usr
    -D CMAKE_BUILD_TYPE=None
    -D CMAKE_INSTALL_LIBDIR=/usr/lib
    -D ADLplug_CHIP=OPL3
    -D ADLplug_USE_SYSTEM_FMT=ON
    -S $_name-$pkgver
    -W no-dev
  )
  local cmake_options_opnplug=(
    -B build-opnplug
    -D CMAKE_INSTALL_PREFIX=/usr
    -D CMAKE_BUILD_TYPE=None
    -D CMAKE_INSTALL_LIBDIR=/usr/lib
    -D ADLplug_CHIP=OPN2
    -D ADLplug_USE_SYSTEM_FMT=ON
    -S $_name-$pkgver
    -W no-dev
  )
  # build opl3
  cmake "${cmake_options_adlplug[@]}"
  cmake --build build-adlplug

  # build opn2
  cmake "${cmake_options_opnplug[@]}"
  cmake --build build-opnplug
}

package_adlplug() {
  pkgdesc="FM synthesizer for ADLMIDI with OPL3 chip emulation"
  depends=(
    adlplug-{lv2,standalone,vst}
  )

  DESTDIR="$pkgdir" cmake --install build-$pkgname
  (
    cd "$pkgdir"
    _pick adlplug-lv2 usr/lib/lv2

    _pick adlplug-standalone usr/bin
    _pick adlplug-standalone usr/share

    _pick adlplug-vst usr/lib/vst
  )

  DESTDIR="$pkgdir" cmake --install build-opnplug
  (
    cd "$pkgdir"
    _pick opnplug-lv2 usr/lib/lv2

    _pick opnplug-standalone usr/bin
    _pick opnplug-standalone usr/share

    _pick opnplug-vst usr/lib/vst
  )
}

package_adlplug-lv2() {
  pkgdesc="FM synthesizer for ADLMIDI with OPL3 chip emulation - LV2 plugin"
  groups=(pro-audio lv2-plugins)
  depends=(
    fmt
    freetype2
    gcc-libs
    glibc
    libx11
    libxext
    lv2-host
  )

  mv -v "$pkgname/"* "$pkgdir"
}

package_adlplug-standalone() {
  pkgdesc="FM synthesizer for ADLMIDI with OPL3 chip emulation - JACK standalone"
  groups=(pro-audio)
  depends=(
    fmt
    freetype2
    gcc-libs
    glibc
    hicolor-icon-theme
    jack
    liblo
    libx11
    libxext
  )
  optdepends=(
    'new-session-manager: for session management'
  )

  mv -v "$pkgname/"* "$pkgdir"
}

package_adlplug-vst() {
  pkgdesc="FM synthesizer for ADLMIDI with OPL3 chip emulation - VST plugin"
  groups=(pro-audio vst-plugins)
  depends=(
    fmt
    freetype2
    gcc-libs
    glibc
    libx11
    libxext
    vst-host
  )

  mv -v "$pkgname/"* "$pkgdir"
}

package_opnplug() {
  pkgdesc="FM synthesizer for OPNMIDI with OPN2 chip emulation"
  depends=(
    opnplug-{lv2,standalone,vst}
  )
}

package_opnplug-lv2() {
  pkgdesc="FM synthesizer for OPNMIDI with OPN2 chip emulation - LV2 plugin"
  groups=(pro-audio lv2-plugins)
  depends=(
    fmt
    freetype2
    gcc-libs
    glibc
    libx11
    libxext
    lv2-host
  )

  mv -v "$pkgname/"* "$pkgdir"
}

package_opnplug-standalone() {
  pkgdesc="FM synthesizer for OPNMIDI with OPN2 chip emulation - JACK standalone"
  groups=(pro-audio)
  depends=(
    fmt
    freetype2
    gcc-libs
    glibc
    hicolor-icon-theme
    jack
    liblo
    libx11
    libxext
  )
  optdepends=(
    'new-session-manager: for session management'
  )

  mv -v "$pkgname/"* "$pkgdir"
}

package_opnplug-vst() {
  pkgdesc="FM synthesizer for OPNMIDI with OPN2 chip emulation - VST plugin"
  groups=(pro-audio vst-plugins)
  depends=(
    fmt
    freetype2
    gcc-libs
    glibc
    libx11
    libxext
    vst-host
  )

  mv -v "$pkgname/"* "$pkgdir"
}

